package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.WorthStatistics;
import sanrenxing.dayEnd.backRepertoryTotal.service.WorthStatisticsService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 净值统计控制器
 */

@RestController
@RequestMapping("/WorthStatisticsController")
public class WorthStatisticsController {

    @Autowired
    private WorthStatisticsService worthStatisticsService;

    /**
     * 显示页面
     * @param limit
     * @param page
     * @return
     */
    @GetMapping("/method")
    public Map<String,Object> queryWorth(int limit,int page){
        Map<String,Object> map=new HashMap<String,Object>();
        List<WorthStatistics> ss=worthStatisticsService.queryWorths(limit,page);
        for (WorthStatistics s : ss) {
            System.out.println(s.getTotalAssets());
        }
        map.put("msg","");
        map.put("code",0);
        map.put("count",worthStatisticsService.count());
        map.put("data",worthStatisticsService.queryWorths(limit,page));
        map.put("shareNumber",worthStatisticsService.shareNumber());
        return map;
    }

    /**
     * 净值统计点击统计时
     * @return
     */
    @PostMapping("/total")
    public String total(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("valuationDate")!=null &&
                session.getAttribute("valuationDate").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            return "already";
        }else {
            session.setAttribute("valuationDate",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            return worthStatisticsService.total()?"success":"fail";
        }
    }

    /**
     * 批量删除
     * @return
     */
    @DeleteMapping("/method/{str}")
    public String monayDelete(@PathVariable("str") String str){
        return worthStatisticsService.monayDelete(str).equalsIgnoreCase("success")?"success":"fail";
    }

    /**
     * 基金资产统计图
     * @param xx 日期
     * @return
     */
    @PostMapping("/xxs/{mm}")
    public Map aqa(@PathVariable("mm") String xx){
       /* {
            name: '总资产',
                    data: [16.3, 12.2, 106.4,16.3,16.3,12.2,106.4]
        }*/

        List<Map> qq=new ArrayList<>();
        List<WorthStatistics> lt=worthStatisticsService.syt(xx);
        List<String> strs=new ArrayList<>();
      Map mx=new HashMap();
       String mzs="";
        for (WorthStatistics jns : lt) {
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
            strs.add(sf.format(jns.getNetDate()));
        }
        for (int i=0;i<3;i++){
            List<Double> jjm=new ArrayList<>();
            Map<String,Object> map=new HashMap<String,Object>();
            for (WorthStatistics jn : lt) {

                if(i==0){
                    mzs="总资产";
                    jjm.add(jn.getTotalAssets());
                }else if(i==1){
                    mzs="资产净值";
                    jjm.add(jn.getNetAsset());
                }else{
                    mzs="总负债";
                    jjm.add(jn.getTotalRespo());
                }
            }
            map.put("name",mzs);
            map.put("data",jjm);
            qq.add(map);
        }
        mx.put("suj",qq);
        mx.put("sj",strs);
               return mx;
    }
    /**
     * 基金净值统计图
     * @param xx 日期
     * @return
     */
    @GetMapping("/zxt/{mm}")
    public Map aqas(@PathVariable("mm") String xx){
        List<WorthStatistics> lt=worthStatisticsService.syts(xx);
        List<String> strs=new ArrayList<>();
        List<Double> du=new ArrayList<>();
        Map mx=new HashMap();
        Map ma=new HashMap();
        String mzs="";
        for (WorthStatistics jns : lt) {
            SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
            strs.add(sf.format(jns.getNetDate()));
            du.add(jns.getAssetValue());
        }
        ma.put("name","单位净值");
        ma.put("data",du);
      List<Map> kk=new ArrayList<>();
      kk.add(ma);
        mx.put("sj",strs);
        mx.put("sjs",kk);
        return mx;
    }
}
